package com.hspedu.reader_;

import org.junit.Test;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

/**
 * @author LiuJinTao
 * @className FileReader_
 * @date 2024/06/27 16:41
 * @description TODO
 */
public class FileReader_ {
    public static void main(String[] args) {

    }

    @Test
    public void fileReader01() {
        String path = "e:\\story.txt";
        FileReader fileReader = null;
        // 创建一个 FileReader 对象
        try {
            fileReader = new FileReader(path);
            // 循环读取
            int data = 0;
            while ((data = fileReader.read()) != -1) {
                System.out.print((char) data);
            }

        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (fileReader != null) {
                try {
                    fileReader.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

    @Test
    public void fileReader02() {
        String path = "e:\\story.txt";
        FileReader fileReader = null;
        // 创建一个 FileReader 对象
        try {
            fileReader = new FileReader(path);
            // 循环读取
            char[] chars = new char[8];
            int readLen = 0;
            while ((readLen = fileReader.read(chars)) != -1) {
                System.out.print(new String(chars, 0, readLen));
            }

        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (fileReader != null) {
                try {
                    fileReader.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}
